package com.mactivity.activities;

import java.util.ArrayList;
import java.util.Map;
import java.util.StringTokenizer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.mactivity.database.DeleteFriendActivityFunctions;
import com.mactivity.helper.Constants;
import com.mactivity.helper.SharedPreferenceHelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
 
public class DeleteFriendActivity extends Activity {
	SharedPreferenceHelper preferenceHelper;
	Integer I;
	String myId,friendId[],friendName[],frId,friendEmail[];
	ArrayList<String>myFriends=new ArrayList<String>();
	ListView friendsList;
	TextView deleteText;
	ArrayAdapter<String> adapter;
	DeleteFriendActivityFunctions deleteActivity= new DeleteFriendActivityFunctions();
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.delete_friend);
        preferenceHelper =  new SharedPreferenceHelper(this);
        Map<String, String> userInfo=preferenceHelper.getUserInformation();
        myId=(userInfo.get(Constants.USER_ID));
        friendId=deleteActivity.getFriendIdList(userInfo.get(Constants.USER_ID));
        if(friendId!=null)
        {
 		System.out.println(friendId.length+" dude wat are you doing??????");
 		friendName=deleteActivity.getFriendNameList(friendId);
 		friendEmail=deleteActivity.getFriendEmailList(friendId);
 		deleteText=(TextView) findViewById(R.id.textDelete);
 		myFriends.clear();
 		for(int i=0;i<friendName.length;i++){
 			System.out.println(friendName[i]+"My friends");
 			myFriends.add(friendName[i]+" ("+friendEmail[i]+")");
 		}
 		
 		adapter=new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1,
				myFriends);
		friendsList = (ListView) findViewById(R.id.friendsView);
		friendsList.setAdapter(adapter);
		friendsList.setTextFilterEnabled(true);
		friendsList.setOnItemClickListener(new OnItemClickListener(){
			
			@Override
			/*
			 * This method fetches the information about the clicked item in the listview 
			 * and asks if user wants to add as a friend 
			 * 
			 */
			public void onItemClick(AdapterView<?> parent,
					View view, int position, long id) {
				String clickedUsersId,clickedName;
				clickedName= myFriends.get(position);
				StringTokenizer st2 = new StringTokenizer(clickedName, "(");
                String tok = null;
                while (st2.hasMoreTokens()) {
                	tok=st2.nextToken();
                }
                System.out.println(tok);
                String res=null;
                res=tok.substring(0, tok.length()-1);
                System.out.println(res);
				System.out.println("Clicked Email: "+clickedName);
				frId=deleteActivity.fetchId(res);
				System.out.println(clickedName+":id=>"+frId);
				AlertDialog alert= new AlertDialog.Builder(DeleteFriendActivity.this).create();
				alert.setTitle("DELETE FRIEND");
				alert.setMessage("Delete "+clickedName+" as your friend");
				alert.setButton("Cancel", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
					}
				});
				alert.setButton2("Delete", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						String returnValue;
						returnValue=deleteActivity.deleteFriend(myId, frId);
						returnValue.replaceAll("\n", "");
						System.out.println("In here:"+returnValue.trim()+"adasdthis line??");
						if(returnValue.trim().equals("1"))
						{
							AlertDialog alert1= new AlertDialog.Builder(DeleteFriendActivity.this).create();
							alert1.setTitle("Successfull");
							alert1.setMessage("Successfully Deleted!! ");
							alert1.setButton2("OK", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
								}
							});
							alert1.show();
						}
						else
						{
							AlertDialog alert2= new AlertDialog.Builder(DeleteFriendActivity.this).create();
							alert2.setTitle("Error");
							alert2.setMessage("Could not delete somehow ");
							alert2.setButton("Dismiss", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
								}
							});
							alert2.show();
						}
					}
				});
				alert.show();
			}
		});
        }
        else
        {
        	System.out.println("Success my friend!!");
        	Toast.makeText(this,
        			"You do not have friends", Toast.LENGTH_LONG).show();
        }
    }
    @Override
    public void onBackPressed()
    {
    	Intent back=new Intent(DeleteFriendActivity.this,HomeActivity.class);
    	startActivity(back);
    	finish();
    }
//  switch ends
}//onclick ends
